package oer.halk.intercept_demo.utils;

import oer.halk.intercept_demo.entity.User;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @Author zhuhao
 * @Date 2022/1/24 14:54
 * @desc
 */
public class ImgThreadDemo {


    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            users.add(user);
        }
        ImgThreadDemo imgThreadDemo = new ImgThreadDemo();

        CompletableFuture<?>[] futures = new CompletableFuture[10];

        ExecutorService es2 = Executors.newFixedThreadPool(10);
        for (int i = 0; i < users.size(); i++) {
            int finalI = i;
            CompletableFuture<Double> voidCompletableFuture = CompletableFuture.supplyAsync(() -> imgThreadDemo.fetchPrice(users.get(finalI)), es2);
            futures[i] = voidCompletableFuture;
        }

        CompletableFuture.allOf(futures).get();
        users.forEach(System.out::println);
    }


    Double fetchPrice(User user) {
        try {
            Thread.sleep(1000);
            user.setPassword("11" + new Random().nextInt());
            user.setUname("dfdf");
        } catch (InterruptedException e) {
        }
        return 5 + Math.random() * 20;
    }
}
